﻿Imports System.Runtime.Serialization
Imports System.Security.Permissions

Namespace Exceptions
    <Serializable()> _
    Public Class WithingsException
        Inherits System.Exception
        Implements ISerializable

        Private m_statusCode As Long

        ''' <summary>
        ''' Gets the WithingsService Error Code after Failure
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property StatusCode() As Long
            Get
                Return m_statusCode
            End Get
        End Property

        ''' <summary>
        ''' Returns a readable message for this Exception
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shadows Function ToString() As String
            Return String.Format(System.Globalization.CultureInfo.CurrentCulture, "{1} - {0}", _
                                 New Object() {Me.Message, CStr(Me.StatusCode)})
        End Function


        ''' <summary>
        ''' Creates as new WithingsException
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            Me.New(String.Empty, -1, Nothing)
        End Sub

        ''' <summary>
        ''' Creates as new WithingsException
        ''' </summary>
        ''' <param name="message">Readable Meassage</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal message As String)
            Me.New(message, -1, Nothing)
        End Sub

        ''' <summary>
        ''' Creates as new WithingsException
        ''' </summary>
        ''' <param name="message">Readable Meassage</param>
        ''' <param name="innerException">Inner Exception</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal message As String, ByVal innerException As Exception)
            Me.New(message, -1, innerException)
        End Sub

        ''' <summary>
        ''' Creates as new WithingsException
        ''' </summary>
        ''' <param name="message">Readable Meassage</param>
        ''' <param name="statusCode">statusCode from the WBSAPI</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal message As String, ByVal statusCode As Long)
            Me.New(message, statusCode, Nothing)
        End Sub
        ''' <summary>
        ''' Creates as new WithingsException
        ''' </summary>
        ''' <param name="message">Readable Meassage</param>
        ''' <param name="statusCode">statusCode from the WBSAPI</param>
        ''' <param name="innerException">Inner Exception</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal message As String, ByVal statusCode As Long, ByVal innerException As Exception)
            MyBase.New(message, innerException)
            m_statusCode = statusCode
        End Sub

        <SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.SerializationFormatter)> _
        Public Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)
            If (info Is Nothing) Then Throw New ArgumentNullException("info")

            info.AddValue("Status", m_statusCode)
            MyBase.GetObjectData(info, context)
        End Sub

        Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
            MyBase.New(info, context)
            If (info Is Nothing) Then Throw New ArgumentNullException("info")
            m_statusCode = info.GetInt32("Status")
        End Sub

    End Class

End Namespace